package com.MSofLectures.logistic;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;

import com.MSofLectures.Entity.User.User;
import com.MSofLectures.Entity.User.UserImp;
import com.MSofLectures.Operator.Lecture.OperatorOnLecture;
import com.MSofLectures.Operator.Lecture.OperatorOnLectureImp;
import com.MSofLectures.Operator.User.OperatorOnUser;
import com.MSofLectures.Operator.User.OperatorOnUserImp;
import com.opensymphony.xwork2.ActionSupport;

public abstract class Basic extends ActionSupport implements ServletRequestAware
{
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Basic.java
	 * @Package com.logistic
	 * @author ufo
	 * 用于处理业务逻辑，实现web Service者均应当集成此类。
	 * 此类对Entity进行操作，以实现与数据库的剥离
	 */

	protected OperatorOnUserImp operatorOnUser=null;
	protected OperatorOnLectureImp operatorOnLecture=null;
	protected HttpServletRequest request;
	protected UserImp user;
	protected Map<String,String> result=new HashMap<String,String>();

	public void finalize() throws Throwable{//为garbage collection准备，无需关注
		if(null!=this.operatorOnLecture)
			this.operatorOnLecture.finalize();
		if(null!=this.operatorOnUser)
			this.operatorOnUser.finalize();
		super.finalize();
	}

	public boolean isOnline(){//是否在线
		return null!=request.getSession().getAttribute("userId");
	}

	public void addUserToSession(String userId,String userPwd){//将用户添加到session
		request.getSession().setAttribute("userId",userId);
		request.getSession().setAttribute("userPwd", userPwd);
	}
	
	public void addUserPriToSeession(String pri){
		request.getSession().setAttribute("userPri", pri);
	}

	public void addSessionToUser(){//将用户从session中取出
		this.user=getUser((String)request.getSession().getAttribute("userId"),
				(String)request.getSession().getAttribute("userPwd"));
	}

	public UserImp getUser(String userId,String userPwd){//使用工厂模式，在这个类的子类当中，如果没有特别情况，请用这个方法来获取user对象
		return new User(userId,userPwd);
	}

	@JSON(name="result")
	abstract public Map<String,String> getResult();//json接口，通过这个接口可以返回json信息
	public void setResult(Map<String, String> result){
		this.result=result;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {//传入request的方法，已经用utf-8编码
		// TODO 自动生成的方法存根
		this.request=request;
		try {
			this.request.setCharacterEncoding("UTF8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	public OperatorOnUserImp getOperatorOnUser() {
		if(null==this.operatorOnUser)
			this.operatorOnUser=new OperatorOnUser();
		return this.operatorOnUser;
	}

	public void setOperatorOnUser(OperatorOnUserImp operatorOnUser) {
		this.operatorOnUser = operatorOnUser;
	}

	public OperatorOnLectureImp getOperatorOnLecture() {
		if(null==this.operatorOnLecture)
			this.operatorOnLecture=new OperatorOnLecture();
		return this.operatorOnLecture;
	}

	public void setOperatorOnLecture(OperatorOnLectureImp operatorOnLecture) {
		this.operatorOnLecture = operatorOnLecture;
	}
}
